package com.android.nearshop.app.adapter.loading;

import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.util.Util;
/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-6
 * @see : 普通载入控制类
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class NormalLoadingView implements LoadingViewInterface
{
    private Activity act;
    private Button mButton;
    private View mLoadingErrView;
    private View mLoadingView;
    private View mMainView;
    private TextView mTextView;
    private View.OnClickListener reTrybuttonClickListener;

    /**
     * 该activity必须包含R.id.base_loading，R.id.base_loading_err，R.id.main的view
     * 和 R.id.btn01的Button，R.id.tv_info的TextView，
     * @param paramActivity
     */
    public NormalLoadingView(Activity paramActivity)
    {
        this.act = paramActivity;
        initComponents();
    }
    
    // 初始化组件
    private void initComponents()
    {
        this.mLoadingView = act.findViewById(R.id.base_loading);
        this.mLoadingErrView = act.findViewById(R.id.base_loading_err);
        this.mMainView =act.findViewById(R.id.main);
        this.mButton = ((Button)act.findViewById(R.id.btn01));
        this.mTextView = ((TextView)act.findViewById(R.id.tv_info));
        this.reTrybuttonClickListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                onRetryClick();
            }
        };
    }
    
    /** 显示网络错误*/
    public void showNetErrView()
    {
        showLoadingErrView(R.string.not_network);
    }
    /** 显示载入错误*/
    public void showLoadingErrView()
    {
        showLoadingErrView(R.string.loading_err);
    }
    /** 显示指定错误*/
    public void showLoadingErrView(int errStrId)
    {
        showLoadingErrView(act.getString(errStrId));
    }
    /** 显示指定错误*/
    public void showLoadingErrView(String errStr)
    {
        this.mTextView.setText(errStr);
        showErrView();
    }
    /** 显示正在载入视图*/
    public void showLoadingView()
    {
        this.mLoadingView.setVisibility(View.VISIBLE);
        this.mLoadingErrView.setVisibility(View.GONE);
        this.mMainView.setVisibility(View.INVISIBLE);
    }
    /** 载入完成后显示主视图*/
    public void showMainView()
    {
        this.mLoadingView.setVisibility(View.GONE);
        this.mLoadingErrView.setVisibility(View.GONE);
        this.mMainView.setVisibility(View.VISIBLE);
    }
    /** 隐藏所有视图*/
    public void hiddenView()
    {
        this.mLoadingView.setVisibility(View.GONE);
        this.mLoadingErrView.setVisibility(View.GONE);
        this.mMainView.setVisibility(View.GONE);
    }
    /** 显示错误重新刷新视图*/
    private void showErrView()
    {
        this.mButton.setOnClickListener(this.reTrybuttonClickListener);
        this.mLoadingView.setVisibility(View.GONE);
        this.mLoadingErrView.setVisibility(View.VISIBLE);
        this.mMainView.setVisibility(View.INVISIBLE);
    }
    
    /** 重新刷新监听器*/
    private void onRetryClick()
    {
        if (!MyApplication.getContext().isNetWorkAvailable())
        {
            Util.showToast(act, R.string.not_network);
            if ((act.getParent() != null)
                    && (act.getParent().getParent() != null)
                    && ((act.getParent().getParent() instanceof IdisplayNetErrAndRefreshView)))
            {
                ((IdisplayNetErrAndRefreshView) act.getParent().getParent()).showNoNetworkDialog();
            }
            if ((act.getParent() != null)
                    && ((act.getParent() instanceof IdisplayNetErrAndRefreshView)))
            {
                ((IdisplayNetErrAndRefreshView) act.getParent()).showNoNetworkDialog();
            }
            if ((this instanceof IdisplayNetErrAndRefreshView))
            {
                ((IdisplayNetErrAndRefreshView) this).showNoNetworkDialog();
            }
        } else
        {
            if ((this instanceof IdisplayNetErrAndRefreshView))
            {
                ((IdisplayNetErrAndRefreshView) this).refreshView();
            }
        }
    }
    
    /** 刷新视图 和 显示 网络异常窗口接口 */
    public static abstract interface IdisplayNetErrAndRefreshView
    {
        public abstract void refreshView();

        public abstract void showNoNetworkDialog();
    }
}